// 利用归并排序  时间:O(nlogn)  空间:O(1)
#include <iostream>
#include <vector>

using namespace std;

long long Merge(vector<int>& v, int l, int mid, int r ){
    vector<int> temp(r-l+1);
	int p1 = l, p2 = mid+1;
	int i = 0;
    long long ret = 0;
	while (p1 <= mid && p2 <= r){
		if (v[p1] <= v[p2]){
            ret += (r-p2+1) * v[p1];
            temp[i++] = v[p1++];
        }
		else
            temp[i++] = v[p2++];
	}
	while (p1 <= mid)
        temp[i++] = v[p1++];
	while (p2 <= r)
        temp[i++] = v[p2++];
		
    for(int i = 0; i < temp.size(); ++i)
        v[l+i] = temp[i];
    
    return ret;
}

long long Process(vector<int>& v, int l, int r){
	if(l >= r)
        return 0;
    int mid = (l&r) + ((l^r)>>1);  //防止溢出
    return Process(v, l, mid) + \
           Process(v, mid+1, r) + \
           Merge(v, l, mid, r);
}

int main()
{
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int i = 0; i < n; ++i)
        cin >> arr[i];
    cout << Process(arr, 0, arr.size()-1);
    return 0;
}